package ee.karileet.ui.catalogue.view.fragment;

import java.util.ResourceBundle;
import java.util.Set;

import com.vaadin.data.util.IndexedContainer;

import ee.karileet.productcatalogue.model.Attribute;
import ee.karileet.ui.catalogue.util.Const;
import ee.karileet.ui.catalogue.util.Translations;

/**
 * Class for displaying product attributes as a table
 * @author Markus Karileet
 *
 */
public class AttributeTable extends ATableView {
	/**
	 * Attributes for table
	 */
	private Set<Attribute> a;
	
	public AttributeTable(Set<Attribute> attributes) {
		a = attributes;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected IndexedContainer containerDatasource() {
		ResourceBundle rb = Translations.get(null, Const.TRANSLATIONS);
		IndexedContainer ic = new IndexedContainer();
		for (String f : getFields()) {
            ic.addContainerProperty(f, String.class, "");
        }
		for (Attribute at : a) {
			Object id = ic.addItem();
			ic.getContainerProperty(id, rb.getString("classifier")).setValue(rb.getString(at.getGroupAttribute().getClassifier()));
			ic.getContainerProperty(id, rb.getString("value")).setValue(at.getValue());
		}
		return ic;
	}

	@Override
	protected String[] getVisibleFields() {
		return getFields();
	}

	@Override
	protected String[] getFields() {
		ResourceBundle rb = Translations.get(null, Const.TRANSLATIONS);
		return  new String[] {
				rb.getString("classifier"),	
				rb.getString("value")
		};
	}

}
